/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { Component } from 'react';
import { Input, Form, message } from 'antd';
import { connect } from 'dva';
import KbChildrenWrapper from '@/components/KbChildrenWrapper';
import KbModal from '@/components/KbModal';
import { names } from '../../config';

const { Item: FormItem } = Form;
const { TextArea } = Input;

@Form.create()
@connect(({ loading }) => ({
  authing: loading.effects['source/auth'],
}))
class Third extends Component {
  static defaultProps = {
    onAdded: () => {},
  };

  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
  }

  //   打开或关闭指定弹窗
  onTriggerModal = open => {
    if (open) {
      const { onCancel } = this.props;
      // 重新授权，先触发取消
      if (onCancel) {
        onCancel().then(({ code }) => {
          if (code > 0) return;
          this.getAuthUrl();
        });
      } else {
        this.getAuthUrl();
      }
    } else {
      this.setState({
        visible: false,
      });
    }
  };

  // 获取授权跳转路径
  getAuthUrl = () => {
    const { type, by, dispatch, authing } = this.props;
    const payload = {};
    switch (type) {
      case 'wuliuyun':
        if (by === 'token') {
          this.setState({
            visible: true,
          });
          return;
        }
        payload.type = 'wuliuyun';
        break;
      case 'taobao':
        payload.type = 'taobao';
        break;
      case 'pindd':
        break;
      case 'pdd_universal':
        payload.state = 'uniweb_pdd_vhome';
        break;
      default:
        break;
    }
    if (authing) {
      message.error('请稍后');
      return;
    }
    dispatch({
      type: 'source/auth',
      payload,
      kind: type,
    });
  };

  // 物流云token提交
  onOk = () => {
    const { form, dispatch, onAdded } = this.props;
    form.validateFields((err, values) => {
      if (err) return;

      dispatch({
        type: 'source/auth',
        payload: {
          ...values,
        },
        kind: 'wuliuyun_token',
      }).then(({ code }) => {
        if (code == 0) {
          this.onTriggerModal(false);
          onAdded();
          dispatch({
            type: 'source/getAllSource',
          });
          // 单号源设置页面，获取最新的下拉框数据
          dispatch({
            type:"setter/getOperatorOrderSourceList",
          })
        }
      });
    });
  };

  render() {
    const { visible } = this.state;
    const { children, form, disabled } = this.props;
    return (
      <>
        <KbChildrenWrapper
          disabled={disabled}
          className="pointer"
          onClick={this.onTriggerModal.bind(this, true)}
        >
          {children}
        </KbChildrenWrapper>
        <KbModal
          centered
          visible={visible}
          onCancel={this.onTriggerModal.bind(this, false)}
          title={`${names.wuliuyun.tag}访问令牌`}
          footer={false}
          onOk={this.onOk}
        >
          <Form>
            <FormItem>
              {form.getFieldDecorator('token', {
                rules: [
                  {
                    required: true,
                    message: '请输入授权Token',
                  },
                ],
              })(
                <TextArea
                  style={{ height: 150 }}
                  placeholder="输入或者粘贴授权Token获取电子面单"
                />
              )}
            </FormItem>
          </Form>
        </KbModal>
      </>
    );
  }
}

export default Third;
